package com.qiumo.gmtool.task;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiumo.gmtool.common.RetResult;
import com.qiumo.gmtool.entity.UserEntity;
import com.qiumo.gmtool.mail.MailParams;
import com.qiumo.gmtool.mail.MailUtil;
import com.qiumo.gmtool.service.UserService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 每月福利
 */
@Slf4j
@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class MonthGoodsTask {

    private final UserService userService;

    private final MailUtil mailUtil;

    //每月1号的0:10:00执行
//    @Scheduled(cron = "0 10 0 1 * ?")
    private void configureTasks() {
        log.info("---------------------开始执行每月福利定时任务");
        //获取所有非超管账号
        List<UserEntity> list = userService.list(new LambdaQueryWrapper<UserEntity>().isNull(UserEntity::getRole));
        if (ObjectUtil.isNotEmpty(list)) {
            for (UserEntity user : list) {
                MailParams params = new MailParams();
                params.setUid(user.getUid());
                //标题
                params.setTitle("旅行者每月福利发放");
                //内容
                params.setContent("旅行者安好，现在为你奉上每月俸禄，祝您旅途愉快。");
                //附件 月卡一张，纪行一张
                params.setItem_list("1202:1,1201:1");
                RetResult retResult = mailUtil.sendMail(params);
                log.info(JSONObject.toJSONString(retResult));
            }
        }
        log.info("---------------------每月福利定时任务执行完毕");
    }
}
